﻿using System.Collections.Generic;
using UnityEngine;

public class Example : MonoBehaviour
{
	public int Row = 10;
	public int Col = 10;
	public static AStarGridMgr AStarGridMgr;

	public Transform start;
	public Transform end;

	/// <summary>
	/// 路径查找器
	/// </summary>
	private PathSeeker seeker;

	private void Start()
	{
		AStarGridMgr = FindObjectOfType<AStarGridMgr>();

		AStarGridMgr.Init();
		AStarGridMgr.GenerateGrid(Row, Col);

		if (seeker == null)
		{
			seeker = gameObject.AddComponent<PathSeeker>();
			//初始化查找完成事件
			seeker.OnFindPath += DrawPath;
		}
	}

	private void Update()
	{
		//更新查找位置
		seeker.SetPosition(start.position, end.position);
	}

	/// <summary>
	/// 画出路径
	/// </summary>
	/// <param name="end"></param>
	public void DrawPath(List<Vector3> posList)
	{
		for (int i = 1, length = posList.Count; i < length; i++)
		{
			Debug.DrawLine(posList[i - 1], posList[i], Color.green, 10);
		}
	}
}
